/**
 * Created by renzhiqiang on 17/4/23.
 */
import * as usersService from './../../services/list/index';
import { staffsSearch } from './../../components/cor/publicFuns'
import pathToRegexp from "path-to-regexp";

export default {
  namespace: 'list',
  state: {
    dataSource: [],
    total: null,
    pageParam: {
      pageNo: 1,
      pageSize: 10,
    },
    foundStage: [],
    sorts:{
      timer: false
    },
    param: {
      projectType: 0,
      status: ''
    },
    filterBoxShowFlag: false,
    loading: false
    // isShow: true
  },
  reducers: {
    initData(state,{}){
      var stat = {
        dataSource: [],
        total: null,
        pageParam: {
          pageNo: 1,
          pageSize: 10,
        },
        foundStage: {},
        filterParam: {},
        param: {
          projectType: 0,
          status: ''
        },
        sorts:{
          timer: false
        },
        filterBoxShowFlag: false,
        loading: false
       };
       return {...state, ...stat};
    },
    save(state, { payload: { data: list, pageParam, param, sorts, loading, filterBoxShowFlag} }) {
      const dataSource= list.data;
      const total = list.totalSize;
      return { ...state, dataSource, pageParam, total, param, sorts, loading, filterBoxShowFlag};
    },
    show(state, { payload : { isShow } }) {
      return {...state, isShow}
    },
    showLoading(state){
      var loading = true;
      return {...state, loading}
    },
    filterBoxShowFlag(state, { preload : { filterBoxShowFlag } }){
      return {...state, filterBoxShowFlag}
    },
    stageList(state, {preload: {foundStage}}){
      return {...state, foundStage}
    },
    setFilterParam(state, action){
      var tmp = state.filterParam;
        tmp[action.payload.name] = action.payload.value;
       return {...state, filterParam: tmp}
    }
  },
  effects: {
    *init({},{put}){
      yield put({
        type: 'initData'
      });
    },
    *fetch({ payload: { pageParam, param, sorts } }, { call, put, select}) {
      yield put({type: 'showLoading'});
      var dataItem =yield select(state => {
        return state.list
      });
      const { data} = yield call(usersService.fetch, { dataItem });
      yield put({
        type: 'save',
        payload: {
          data,
          pageParam: pageParam,
          param: param,
          sorts: sorts,
          filterBoxShowFlag: false,
          loading: false
        },
      });
    },
    *stage({}, { call, put, select}) {
      const data = yield call(staffsSearch, 'fund_type');
      debugger;
      let tmp = data.data.data.map(x => {
        return {text: x.name, value: x.id}
      });
      yield put({
        type: 'stageList',
        preload: {
          foundStage: tmp
        }
      });
    },
    *filterBoxShowFlagChange({payload: { filterBoxShowFlag }},{put}){
      debugger;
      yield put({
        type: 'filterBoxShowFlag',
        preload: {
          filterBoxShowFlag: filterBoxShowFlag
        }
      });
    }
  },
  subscriptions: {
    setup({ dispatch, history }) {
      return history.listen(({ pathname, query }) => {
        const match = pathToRegexp('/home').exec(pathname);
        if (match) {
          dispatch({ type: 'init', payload: {} });
          dispatch({
            type:'stage'
          });
          dispatch({ type: 'fetch', payload: {
            pageParam: {
              pageNo: 1,
              pageSize: 10,
            },
            param: {
              projectType: 0,
              status: ''
            },
            sorts:{
              timer: false
            },
          } });
        };
      });
    },
  },
};
